package com.adulcimer.core.tools;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.Generated;

/**
 * @author Simon
 * @create 2025/2/6
 * @Description
 **/

@JsonInclude(Include.NON_NULL)
@ApiResponse(
        responseCode = "200",
        description = "返回模型"
)
public class R<T> {
    @Schema(
            description = "返回码"
    )
    public int code;
    @Schema(
            description = "返回数据"
    )
    public T data;
    @Schema(
            description = "自定义消息"
    )
    public String message;

    public static <T> R<T> ok() {
        return ok(null);
    }

    public static <T> R<T> ok(T data) {
        return ok(data, "success");
    }

    public static <T> R<T> ok(T data, String message) {
        R<T> response = new R();
        response.code = 1;
        response.data = data;
        response.message = message;
        return response;
    }

    public static <T> R<T> error(String message, T data) {
        R<T> response = new R();
        response.code = 3;
        response.data = data;
        response.message = message;
        return response;
    }

    public static <T> R<T> error(String message) {
        return error(message);
    }

    public static <T> R<T> error() {
        return error("");
    }

    public static <T> R<T> systemError(String message, T data) {
        R<T> response = new R();
        response.code = 2;
        response.data = data;
        response.message = message;
        return response;
    }

    public static <T> R<T> systemError(String message) {
        return systemError(message,null);
    }

    public static <T> R<T> errorCode(DemonErrorCode code) {
        R<T> response = new R();
        response.code = code.getCode();
        response.data = null;
        response.message = code.getMessage();
        return response;
    }

    public static <T> R<T> errorCode(DemonErrorCode code, T data) {
        R<T> response = new R();
        response.code = code.getCode();
        response.data = data;
        response.message = code.getMessage();
        return response;
    }

    public static <T> R<T> errorNoAuth(String message) {
        R<T> response = new R();
        response.code = 401;
        response.data = null;
        response.message = message;
        return response;
    }

    public static <T> R<T> errorNoAuth() {
        R<T> response = new R();
        response.code = 401;
        response.data = null;
        response.message = "No Auth";
        return response;
    }

    @Generated
    public R() {
    }

    @Generated
    public int getCode() {
        return this.code;
    }

    @Generated
    public T getData() {
        return this.data;
    }

    @Generated
    public String getMessage() {
        return this.message;
    }

    @Generated
    public void setCode(int code) {
        this.code = code;
    }

    @Generated
    public void setData(T data) {
        this.data = data;
    }

    @Generated
    public void setMessage(String message) {
        this.message = message;
    }

    @Generated
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof R)) {
            return false;
        } else {
            R<?> other = (R)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.getCode() != other.getCode()) {
                return false;
            } else {
                Object this$data = this.getData();
                Object other$data = other.getData();
                if (this$data == null) {
                    if (other$data != null) {
                        return false;
                    }
                } else if (!this$data.equals(other$data)) {
                    return false;
                }

                Object this$message = this.getMessage();
                Object other$message = other.getMessage();
                if (this$message == null) {
                    if (other$message != null) {
                        return false;
                    }
                } else if (!this$message.equals(other$message)) {
                    return false;
                }

                return true;
            }
        }
    }

    @Generated
    protected boolean canEqual(Object other) {
        return other instanceof R;
    }

    @Generated
    public int hashCode() {
        boolean PRIME = true;
        int result = 1;
        result = result * 59 + this.getCode();
        Object $data = this.getData();
        result = result * 59 + ($data == null ? 43 : $data.hashCode());
        Object $message = this.getMessage();
        result = result * 59 + ($message == null ? 43 : $message.hashCode());
        return result;
    }

    @Generated
    public String toString() {
        int var10000 = this.getCode();
        return "R(code=" + var10000 + ", data=" + String.valueOf(this.getData()) + ", message=" + this.getMessage() + ")";
    }
}
